package com.avcdata.aoscustommade.utils;

import android.graphics.Color;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Text相关工具类
 */
public class TextUitl {
    private TextUitl() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * 检测是否有emoji表情
     *
     * @param source
     * @return
     */
    public static boolean containsEmoji(String source) {
        int len = source.length();
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (!isEmojiCharacter(codePoint)) { //如果不能匹配,则该字符是Emoji表情
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否是Emoji
     *
     * @param codePoint 比较的单个字符
     * @return
     */
    public static boolean isEmojiCharacter(char codePoint) {
        return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) ||
                (codePoint == 0xD) || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
                ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000)
                && (codePoint <= 0x10FFFF));
    }


    /**
     * 字符创是否为空
     *
     * @param string
     * @return
     */
    public static boolean isEmpty(String string) {
        if (string.isEmpty() || string == null || string.equals("null") || string.equals("")) {
//            Log.e("is", "---------字符串是空的--------" + string);
            return true;
        }
        return false;
    }


    /**
     * 是否是手机号
     *
     * @param phone
     * @return
     */
    public static boolean isPhone(String phone) {
        Pattern p = Pattern.compile(AvcConstants.CHINA_MOBILE_PATTERN);
        Pattern p1 = Pattern.compile(AvcConstants.CHINA_TELECOM_PATTERN);
        Pattern p2 = Pattern.compile(AvcConstants.CHINA_UNICOM_PATTERN);
        Matcher m = p.matcher(phone.trim());
        Matcher m1 = p1.matcher(phone.trim());
        Matcher m2 = p2.matcher(phone.trim());
        if (m.matches() || m1.matches() || m2.matches()) {
            return true;
        }
        return false;
    }


    /**
     * 是否是邮箱
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        Pattern p = Pattern.compile(AvcConstants.CHINA_EMAIL_PATTERN);
        Matcher m = p.matcher(email);
        if (m.matches()) {
            return true;
        }
        return false;
    }


    /**
     * 电话隐藏中间
     *
     * @param phone
     */
    public static String phoneHide(String phone) {
        return phone.trim().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }



    /**
     * 是否是这个设备
     * @param sp
     */
    public static boolean isDevice(SharedPreferencesUtils sp) {





        return false;
    }





    /**
     * 提取控件中的字符串
     *
     * @param view
     */
    public static String getString(TextView view) {
        return view.getText().toString().trim();
    }

    /**
     * 提取控件中的字符串
     *
     * @param view
     */
    public static String getString(EditText view) {
        return view.getText().toString().trim();
    }


    /**
     * 保留二位小数
     */
    public static String retainTwoDecimals(String string) {
        double temp = Double.valueOf(string);
        if (temp< 0){
            return new BigDecimal(Math.abs(temp)).setScale(1, BigDecimal.ROUND_HALF_UP)+"";
        }else {
            return new BigDecimal(string).setScale(1, BigDecimal.ROUND_HALF_UP)+"";
        }
    }

    public static String retainRound(String string) {
        double temp = Double.valueOf(string);
        if (temp< 0){
            return new BigDecimal(Math.abs(temp)).setScale(0, BigDecimal.ROUND_HALF_UP)+"";
        }else {
            return new BigDecimal(string).setScale(0, BigDecimal.ROUND_HALF_UP)+"";
        }
    }




    /**
     * 保留二位小数
     */
    public static Float retainTwoDecimalsFloat(String string) {
        double temp = Double.valueOf(string);
        if (temp< 0){
            return Float.valueOf(new BigDecimal(Math.abs(temp)).setScale(1, BigDecimal.ROUND_HALF_UP)+"");
        }else {
            return Float.valueOf(new BigDecimal(string).setScale(1, BigDecimal.ROUND_HALF_UP)+"");
        }
    }

    /**
     * 保留二位小数
     */
    public static String retainTwoDecimalsString(Float string) {
        if (string< 0){
            return new BigDecimal(Math.abs(string)).setScale(1, BigDecimal.ROUND_HALF_UP)+"";
        }else {
            return new BigDecimal(string).setScale(1, BigDecimal.ROUND_HALF_UP)+"";
        }
    }

    /**
     * 保留二位小数
     */
    public static String retainTwoDecimalsStringPie(String string) {
//        Log.e("string","--------保留小数-------"+string);
        double temp = Double.valueOf(string);
        if (temp< 0){
            return new BigDecimal(Math.abs(temp)).setScale(1, BigDecimal.ROUND_HALF_UP)+"";
        }else {
            return new BigDecimal(string).setScale(1, BigDecimal.ROUND_HALF_UP)+"";
        }

    }


    /**
     * 转化超大数据
     */
    public static String getSuperBig(String string) {
        double temp = Double.valueOf(string);
        if (temp< 0){
            return new BigDecimal(Math.abs(temp)).setScale(9, BigDecimal.ROUND_HALF_UP)+"";
        }else {
            return new BigDecimal(string).setScale(9, BigDecimal.ROUND_HALF_UP)+"";
        }

    }



    /**
     * 格式化日期
     */
    public static String format00(String string) {
        DecimalFormat df = new DecimalFormat("00");
        return df.format(Double.valueOf(string));
    }


    /**
     * 判断是否上升
     */
    public static boolean isUp(String string) {
        if (Float.valueOf(string) > 0) {
            return true;
        }
        return false;
    }


    /**
     * 获取日期剩余周数
     */
    public static int getSurplusWeek(int count,String newWeek) {
        if (isEmpty(newWeek)) return 0;
        String[] arrayNewWeek = newWeek.split("W");
        int temp = count - Integer.valueOf(arrayNewWeek[1]);
        if (temp>0){
            return Integer.valueOf(arrayNewWeek[1]) - 1;
        }
            return count - 1;
    }


    /**
     * 获取日期差值周数
     */
    public static int getStartEndWeek(String startWeek,String endWeek) {
        int temp = 0;
        if (isEmpty(startWeek) || isEmpty(endWeek) || startWeek.equals(endWeek)) return temp;
        String[] arrayStartWeek = startWeek.split("W");
        String[] arrayEndWeek = endWeek.split("W");
        int arrayStartWeek0 = Integer.valueOf(arrayStartWeek[0]);
        int arrayStartWeek1 = Integer.valueOf(arrayStartWeek[1]);
        int arrayEndWeek0 = Integer.valueOf(arrayEndWeek[0]);
        int arrayEndWeek1 = Integer.valueOf(arrayEndWeek[1]);
        if (arrayStartWeek0 < arrayEndWeek0){
            int tempMultiple = arrayEndWeek0 - arrayStartWeek0;
            return (52 * tempMultiple)-arrayStartWeek1+arrayEndWeek1 ;

        }else {
            return arrayEndWeek1 - arrayStartWeek1 ;
        }
    }



    /**
     * 判断日期大小的
     */
    public static boolean isStartThanEndWeek(String startWeek,String endWeek) {
        if (isEmpty(startWeek) || isEmpty(endWeek) || startWeek.equals(endWeek)) return false;
        String[] arrayStartWeek = startWeek.split("W");
        String[] arrayEndWeek = endWeek.split("W");
        int arrayStartWeek0 = Integer.valueOf(arrayStartWeek[0]);
        int arrayStartWeek1 = Integer.valueOf(arrayStartWeek[1]);
        int arrayEndWeek0 = Integer.valueOf(arrayEndWeek[0]);
        int arrayEndWeek1 = Integer.valueOf(arrayEndWeek[1]);
        if (arrayStartWeek0 > arrayEndWeek0){
            return  true;
        }else {
            if (arrayStartWeek0 == arrayEndWeek0){
                if (arrayStartWeek1 > arrayEndWeek1){
                    return  true;
                }else {
                    return  false;
                }
            }
            return false;
        }

    }


    /**
     * 获取过大数据截取
     */
    public static String getOutBigData(String count) {


//        if (count.contains("E")){
//            String [] array = count.split("E");
//            if (array[1].equals("6")){
//                count = Math.round(Double.valueOf(array[0])*100)+"";
//            }else if (array[1].equals("7")){
//                count = Math.round(Double.valueOf(array[0])*1000)+"";
//            }else if (array[1].equals("8")){
//                count = Math.round(Double.valueOf(array[0])*10000)+"";
//            }else if (array[1].equals("9")){
//                count = Math.round(Double.valueOf(array[0])*100000)+"";
//            }
//            return count;
//    }
//    return Math.round(Double.valueOf(count)/10000)+"";
    return new BigDecimal(count).setScale(1, BigDecimal.ROUND_HALF_UP)+"";
    }


    /**
     * 获取销额转换数据
     */
    public static String getSalesChange(String count) {
            Float temp  = 0f;
            String [] array = count.split("E");
            if (array[1].equals("6")){
                temp = Float.valueOf(Float.valueOf(array[0])*10*6);
            }else if (array[1].equals("7")){
                temp = Float.valueOf(Float.valueOf(array[0])*10*7);
            }else if (array[1].equals("8")){
                temp = Float.valueOf(Float.valueOf(array[0])*10*8);
            }else if (array[1].equals("9")){
                temp = Float.valueOf(Float.valueOf(array[0])*10*9);
            }else if (array[1].equals("5")){
                temp = Float.valueOf(Float.valueOf(array[0])*10*5);
            }

        return temp+"";
    }


    /**
     * 获取销额转换数据
     */
    public static boolean isBig10000(String count) {
//        Log.e("string","--------比较的数据是否大于万-------"+count);
        if (Math.round(Float.valueOf(count))>=10000){
            return true;
        }else {
            return false;
        }
    }




    /**
     * 数据是否大于亿
     */
    public static boolean isBigE(String count) {
        if (count.contains("E")){
            return true;
        }
        return false;
    }




    /**
     * 转换成万元
     */
    public static String transformThousand(String string) {
        double num = Double.valueOf(string);
        if (num < 100000) {
            return retainTwoDecimals(String.valueOf(num));
        } else {
            return retainTwoDecimals(String.valueOf(num / 10000));
        }

    }

    /**
     * 颜色
     */
    public static ArrayList<Integer> getColor() {
        ArrayList<Integer> colors = new ArrayList<>();
        colors.add(Color.parseColor("#FF0000"));
        colors.add(Color.parseColor("#FFA500"));
        colors.add(Color.parseColor("#ee64f7"));
        colors.add(Color.parseColor("#006400"));
        colors.add(Color.parseColor("#00FFFF"));
        colors.add(Color.parseColor("#0000FF"));
        colors.add(Color.parseColor("#800080"));
        colors.add(Color.parseColor("#FFFF00"));
        colors.add(Color.parseColor("#7FFF00"));
        return colors;
    }


    /**
     * 字体颜色
     * @return
     */
    public static ArrayList<Integer> getColorText() {
        ArrayList<Integer> colors = new ArrayList<>();
        colors.add(Color.parseColor("#f7b400"));
        colors.add(Color.parseColor("#0a4678"));
        return colors;
    }


}

